package net.sunofbeaches.services.impl;

import net.sunofbeaches.dao.LooperDao;
import net.sunofbeaches.pojo.Looper;
import net.sunofbeaches.pojo.SobUser;
import net.sunofbeaches.response.ResponseResult;
import net.sunofbeaches.services.ILoopService;
import net.sunofbeaches.services.IUserService;
import net.sunofbeaches.utils.Constants;
import net.sunofbeaches.utils.IdWorker;
import net.sunofbeaches.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.xml.soap.Text;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ILoopServicesImpl extends BaseService implements ILoopService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private LooperDao looperDao;
    @Autowired
    private IUserService iUserService;

    @Override
    public ResponseResult addLoop(Looper looper) {
        String title = looper.getTitle();
        if (TextUtils.isEmpty(title)) {
            return ResponseResult.FAILED("标题不可以为空.");
        }
        String imageUrl = looper.getImageUrl();
        if (TextUtils.isEmpty(imageUrl)) {
            return ResponseResult.FAILED("图片不可以为空.");
        }
        String targetUrl = looper.getTargetUrl();
        if (TextUtils.isEmpty(targetUrl)) {
            return ResponseResult.FAILED("跳转链接不可以为空.");
        }
        looper.setId(idWorker.nextId() + "");
        looper.setCreateTime(new Date());
        looper.setUpdateTime(new Date());
        looperDao.save(looper);
        return ResponseResult.SUCCESS("轮播图添加成功.");
    }

    @Override
    public ResponseResult updateLoop(String lookId, Looper looper) {
        Looper looperDb = looperDao.findOneById(lookId);
        if (looperDb == null) {
            return ResponseResult.FAILED("轮播图不存在");
        }
        //不可以为空的，要判空
        String title = looper.getTitle();
        if (!TextUtils.isEmpty(title)) {
            looperDb.setTitle(title);
        }
        String imageUrl = looper.getImageUrl();
        if (!TextUtils.isEmpty(imageUrl)) {
            looperDb.setImageUrl(imageUrl);
        }
        if (!TextUtils.isEmpty(looper.getState())) {
            looperDb.setState(looper.getState());
        }
        looperDb.setOrder(looper.getOrder());
        looperDb.setUpdateTime(new Date());
        //可以为空的直接设置
        //保存回去
        looperDao.save(looperDb);
        return ResponseResult.SUCCESS("轮播图更新成功.");
    }

    @Override
    public ResponseResult deleteLoop(String loopId) {
        looperDao.deleteById(loopId);
        return ResponseResult.SUCCESS("删除成功.");
    }

    @Override
    public ResponseResult listLoops() {
        SobUser sobUser = iUserService.checkSobUser();
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        List<Looper> all;
        if (sobUser == null || !Constants.User.ROLE_ADMIN.equals(sobUser.getRoles())) {
            all = looperDao.listLooperByState("1");
        } else {
            all = looperDao.findAll(sort);
        }
        return ResponseResult.SUCCESS("获取轮播图列表才公平.").setData(all);
    }

    @Override
    public ResponseResult getLoop(String loopId) {
        Looper looper = looperDao.findOneById(loopId);
        if (looper == null) {
            return ResponseResult.FAILED("轮播图不存在.");
        }
        return ResponseResult.SUCCESS("获取轮播图成功.").setData(looper);
    }


}
